package com.face.server.controller;

import com.face.RespMsg;
import com.face.server.entity.Role;
import com.face.server.repository.RoleRepository;
import com.face.server.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * Created by ryan on 2018/1/29.
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private RoleService roleService;

    @GetMapping(value = {"/listRoles"})
    public RespMsg listRoles(@PageableDefault(sort = "name",size = 10000)Pageable pageable, @RequestParam(required = false)String searchKey){
        return RespMsg.build(roleService.listRoles(pageable,searchKey));
    }

    @DeleteMapping("/deleteRole/{rid}")
    public RespMsg deleteRole(@PathVariable String rid){
        Role role=roleRepository.findOne(rid);
        if (role==null)
            return RespMsg.error(-1,"未找到角色");
        roleRepository.delete(rid);
        return RespMsg.build(null);
    }

    @PutMapping("/editAdminsInRole/{rid}")
    public RespMsg editAdminsInRole(@PathVariable String rid,@RequestBody(required =false) String searchData){
        if (roleRepository.findOne(rid)==null)
            return RespMsg.error(-1,"未找到改角色");
        return RespMsg.build(roleService.editAdminsInRole(rid,searchData));
    }

    @PutMapping("/editFuncsInRole/{rid}")
    public RespMsg editFuncsInRole(@PathVariable String rid,@RequestBody(required = false) String searchData){
        if (roleRepository.findOne(rid)==null)
            return RespMsg.error(-1,"未找到改角色");
        return RespMsg.build(roleService.editFuncsInRole(rid,searchData));
    }

    @PostMapping("/createRole/{name}")
    public RespMsg createRole(@PathVariable String name){
        return RespMsg.build(roleService.createRole(name));
    }

    @GetMapping("/listOwnAdmins/{id}")
    public RespMsg listOwnnAdmins(@PathVariable String id){
        return RespMsg.build(roleRepository.findOne(id));
    }

    @GetMapping("/listMyRole")
    public RespMsg listMyRole(){
        return RespMsg.build(roleService.listMyRole());
    }
}
